"""
赋值运算符
学习目标：
1. 掌握基本赋值和复合赋值运算符
2. 理解赋值运算符的简化作用
3. 学会在实际场景中使用赋值运算符
"""

# ==================== 1. 基本赋值运算符 ====================
print("=" * 50)
print("1. 基本赋值运算符 =")
print("=" * 50)

# 基本赋值
x = 10
print(f"x = 10，x的值为：{x}")

# 可以同时给多个变量赋值
a, b, c = 1, 2, 3
print(f"a = {a}, b = {b}, c = {c}")

# 多个变量赋相同的值
x = y = z = 100
print(f"x = y = z = 100，结果：x={x}, y={y}, z={z}")

print()

# ==================== 2. 复合赋值运算符 ====================
print("=" * 50)
print("2. 复合赋值运算符")
print("=" * 50)

# 加法赋值 +=
x = 10
print(f"初始值：x = {x}")
x += 5  # 等价于 x = x + 5
print(f"x += 5 后，x = {x}")

# 减法赋值 -=
x -= 3  # 等价于 x = x - 3
print(f"x -= 3 后，x = {x}")

# 乘法赋值 *=
x *= 2  # 等价于 x = x * 2
print(f"x *= 2 后，x = {x}")

# 除法赋值 /=
x /= 4  # 等价于 x = x / 4
print(f"x /= 4 后，x = {x}")

print()

# ==================== 3. 更多复合赋值运算符 ====================
print("=" * 50)
print("3. 更多复合赋值运算符")
print("=" * 50)

# 整除赋值 //=
x = 20
print(f"初始值：x = {x}")
x //= 3  # 等价于 x = x // 3
print(f"x //= 3 后，x = {x}")

# 取余赋值 %=
x %= 5  # 等价于 x = x % 5
print(f"x %= 5 后，x = {x}")

# 幂赋值 **=
x **= 3  # 等价于 x = x ** 3
print(f"x **= 3 后，x = {x}")

print()

# ==================== 4. 复合赋值运算符对比 ====================
print("=" * 50)
print("4. 普通赋值 vs 复合赋值")
print("=" * 50)

# 方式1：普通赋值
count1 = 10
count1 = count1 + 1
print(f"普通赋值：count1 = count1 + 1，结果：{count1}")

# 方式2：复合赋值（更简洁）
count2 = 10
count2 += 1
print(f"复合赋值：count2 += 1，结果：{count2}")

print("\n💡 复合赋值运算符更简洁，是推荐的写法")

print()

# ==================== 5. 实战：计数器 ====================
print("=" * 50)
print("5. 实战：计数器")
print("=" * 50)

# 访问量统计
visitor_count = 0
print(f"初始访问量：{visitor_count}")

# 有人访问
visitor_count += 1
print(f"第1位访客，当前访问量：{visitor_count}")

visitor_count += 1
print(f"第2位访客，当前访问量：{visitor_count}")

# 一次性来了10位访客
visitor_count += 10
print(f"又来了10位访客，当前访问量：{visitor_count}")

# 晚上清零（重新赋值）
visitor_count = 0
print(f"第二天，访问量清零：{visitor_count}")

print()

# ==================== 6. 实战：累加求和 ====================
print("=" * 50)
print("6. 实战：累加求和")
print("=" * 50)

# 购物车总价计算
total = 0
print(f"购物车初始金额：¥{total}")

# 添加商品
item1_price = 39.9
total += item1_price
print(f"添加商品1（¥{item1_price}），总计：¥{total}")

item2_price = 129.5
total += item2_price
print(f"添加商品2（¥{item2_price}），总计：¥{total}")

item3_price = 88.0
total += item3_price
print(f"添加商品3（¥{item3_price}），总计：¥{total}")

print(f"\n购物车总价：¥{total}")

# 应用折扣
total *= 0.9  # 9折
print(f"应用9折后：¥{total:.2f}")

print()

# ==================== 7. 实战：成绩统计 ====================
print("=" * 50)
print("7. 实战：学生成绩统计")
print("=" * 50)

# 初始化
total_score = 0
subject_count = 0

print("输入各科成绩：")

# 数学成绩
math = 85
total_score += math
subject_count += 1
print(f"数学：{math}分")

# 英语成绩
english = 92
total_score += english
subject_count += 1
print(f"英语：{english}分")

# 语文成绩
chinese = 78
total_score += chinese
subject_count += 1
print(f"语文：{chinese}分")

# 物理成绩
physics = 88
total_score += physics
subject_count += 1
print(f"物理：{physics}分")

# 计算平均分
average = total_score / subject_count
print(f"\n总分：{total_score}")
print(f"科目数：{subject_count}")
print(f"平均分：{average:.2f}")

print()

# ==================== 8. 实战：库存管理 ====================
print("=" * 50)
print("8. 实战：商品库存管理")
print("=" * 50)

product_name = "Python编程书"
stock = 100  # 初始库存

print(f"商品：{product_name}")
print(f"初始库存：{stock}")
print()

# 销售出库
sold = 15
stock -= sold
print(f"销售{sold}本，剩余库存：{stock}")

sold = 8
stock -= sold
print(f"销售{sold}本，剩余库存：{stock}")

# 补货入库
restock = 50
stock += restock
print(f"补货{restock}本，剩余库存：{stock}")

# 库存预警
if stock < 20:
    print("⚠️ 库存不足，请及时补货！")
elif stock < 50:
    print("⚠️ 库存偏低")
else:
    print("✅ 库存充足")

print()

# ==================== 9. 实战：游戏分数系统 ====================
print("=" * 50)
print("9. 实战：游戏分数系统")
print("=" * 50)

player_name = "玩家1"
score = 0
level = 1

print(f"玩家：{player_name}")
print(f"初始分数：{score}")
print(f"初始等级：{level}")
print()

# 完成任务1
task1_score = 100
score += task1_score
print(f"完成任务1，获得{task1_score}分，当前分数：{score}")

# 完成任务2
task2_score = 150
score += task2_score
print(f"完成任务2，获得{task2_score}分，当前分数：{score}")

# 击败boss，分数翻倍
score *= 2
print(f"击败Boss，分数翻倍，当前分数：{score}")

# 升级
level += 1
print(f"升级！当前等级：{level}")

# 失误扣分
penalty = 50
score -= penalty
print(f"失误，扣除{penalty}分，当前分数：{score}")

print(f"\n最终 - 等级：{level}，分数：{score}")

print()

# ==================== 10. 实战：银行账户 ====================
print("=" * 50)
print("10. 实战：银行账户操作")
print("=" * 50)

account_name = "张三"
balance = 1000.0  # 初始余额

print(f"账户：{account_name}")
print(f"初始余额：¥{balance}")
print()

# 存款
deposit = 500
balance += deposit
print(f"存入¥{deposit}，当前余额：¥{balance}")

# 取款
withdrawal = 200
balance -= withdrawal
print(f"取出¥{withdrawal}，当前余额：¥{balance}")

# 利息（年利率3%）
interest_rate = 0.03
interest = balance * interest_rate
balance += interest
print(f"获得利息¥{interest:.2f}，当前余额：¥{balance:.2f}")

# 手续费
fee = 5
balance -= fee
print(f"扣除手续费¥{fee}，当前余额：¥{balance:.2f}")

print(f"\n最终余额：¥{balance:.2f}")

print()

# ==================== 11. 实战：字符串拼接 ====================
print("=" * 50)
print("11. 实战：字符串累加")
print("=" * 50)

# 构建一个完整的句子
message = ""
print(f"初始字符串：'{message}'")

message += "Python"
print(f"添加'Python'后：'{message}'")

message += " is"
print(f"添加' is'后：'{message}'")

message += " awesome"
print(f"添加' awesome'后：'{message}'")

message += "!"
print(f"添加'!'后：'{message}'")

print(f"\n最终字符串：'{message}'")

print()

# 构建多行文本
text = ""
text += "第一行\n"
text += "第二行\n"
text += "第三行\n"
print("多行文本：")
print(text)

print()

# ==================== 12. 实战：列表累加 ====================
print("=" * 50)
print("12. 实战：列表累加")
print("=" * 50)

# 收集学生姓名
students = []
print(f"初始列表：{students}")

students += ["张三"]
print(f"添加'张三'：{students}")

students += ["李四"]
print(f"添加'李四'：{students}")

students += ["王五", "赵六"]
print(f"添加'王五'和'赵六'：{students}")

print(f"\n学生名单：{students}")
print(f"总人数：{len(students)}")

print()

# ==================== 13. 综合实战：步数统计 ====================
print("=" * 50)
print("13. 综合实战：每日步数统计")
print("=" * 50)

target_steps = 10000  # 目标步数
current_steps = 0  # 当前步数

print(f"今日目标：{target_steps}步")
print(f"初始步数：{current_steps}")
print()

# 上午
morning_steps = 3500
current_steps += morning_steps
print(f"上午运动，步数+{morning_steps}，当前：{current_steps}步")

# 中午
noon_steps = 1200
current_steps += noon_steps
print(f"中午散步，步数+{noon_steps}，当前：{current_steps}步")

# 下午
afternoon_steps = 2800
current_steps += afternoon_steps
print(f"下午活动，步数+{afternoon_steps}，当前：{current_steps}步")

# 晚上
evening_steps = 3000
current_steps += evening_steps
print(f"晚上锻炼，步数+{evening_steps}，当前：{current_steps}步")

# 统计
print(f"\n今日总步数：{current_steps}")
print(f"目标步数：{target_steps}")

if current_steps >= target_steps:
    over_steps = current_steps - target_steps
    print(f"🎉 恭喜！已完成目标，超出{over_steps}步")
else:
    remaining = target_steps - current_steps
    print(f"💪 加油！还差{remaining}步达成目标")

# 完成率
completion_rate = (current_steps / target_steps) * 100
print(f"完成率：{completion_rate:.1f}%")

print()

# ==================== 总结 ====================
print("=" * 50)
print("✨ 赋值运算符总结")
print("=" * 50)
print("""
1. 基本赋值：=
   x = 10

2. 复合赋值运算符：
   +=   加法赋值     x += 5   →   x = x + 5
   -=   减法赋值     x -= 3   →   x = x - 3
   *=   乘法赋值     x *= 2   →   x = x * 2
   /=   除法赋值     x /= 4   →   x = x / 4
   //=  整除赋值     x //= 3  →   x = x // 3
   %=   取余赋值     x %= 5   →   x = x % 5
   **=  幂赋值       x **= 2  →   x = x ** 2

💡 优点：
- 代码更简洁
- 减少重复输入
- 提高代码可读性

💡 常用场景：
- 计数器：count += 1
- 累加求和：total += value
- 库存管理：stock -= sold
- 字符串拼接：text += "新内容"
- 列表扩展：list += [item]

⚠️ 注意事项：
- 赋值是 =，比较是 ==
- 复合赋值会改变原变量的值
- 字符串和列表也可以使用 +=
""")
print("=" * 50)

